namespace CIS.Customer
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerInfoTravelPage : Page
    {
        protected TextBox CouponTxb;
        protected CheckBox EditChk;
        protected Image iBtnCustomer;
        protected ImageButton iBtnGray4;
        protected ImageButton iBtnOrange0;
        protected ImageButton iBtnOrange1;
        protected ImageButton iBtnOrange2;
        protected ImageButton iBtnOrange3;
        protected ImageButton iBtnOrange5;
        protected ImageButton iBtnOrange6;
        protected ImageButton iBtnOrange7;
        protected ImageButton iBtnOrange8;
        protected Label lbCoupon;
        protected Label lbEdit;
        protected Label lbOther;
        protected Label lbSaveMsg;
        protected Label lbTranSportTool;
        protected TextBox OtherTxb;
        protected UserPrincipal Principal = null;
        protected Repeater rptTravel;
        protected Button SaveBtn;
        protected DropDownList TransportToolUsedDdl;

        protected string AddStrPrefix(object obj, string strPre)
        {
            if (obj.ToString() == string.Empty)
            {
                return "";
            }
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            return (ManageSystemConfig.GetSystemSetLanguage("CustomerInfoTravelPage", strPre, language, path) + obj.ToString());
        }

        protected string DecryptString(object obj)
        {
            return ManageOther.DecodeWithBytes(obj.ToString());
        }

        private void FillPage()
        {
            Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
            for (int i = 0; i < this.TransportToolUsedDdl.Items.Count; i++)
            {
                if (this.TransportToolUsedDdl.Items[i].Value == ManageCustomer.GetCustomerTravelInfoOfTranTool(customerId))
                {
                    this.TransportToolUsedDdl.SelectedIndex = i;
                    break;
                }
            }
            if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
            {
                this.CouponTxb.ReadOnly = true;
                this.lbEdit.Visible = false;
                this.EditChk.Visible = false;
                this.OtherTxb.ReadOnly = true;
                this.SaveBtn.Visible = false;
            }
            this.RptDataBind();
        }

        private void iBtnOrange0_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerBasicInfoPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoPersonalPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange2_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoPreferencePage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange3_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoHealthCostumePage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange5_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoStudyPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange6_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoWorkPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange7_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoFamilyPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange8_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/CIS/Customer/CustomerInfoOtherPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void InitializeComponent()
        {
            this.iBtnOrange0.Click += new ImageClickEventHandler(this.iBtnOrange0_Click);
            this.iBtnOrange1.Click += new ImageClickEventHandler(this.iBtnOrange1_Click);
            this.iBtnOrange2.Click += new ImageClickEventHandler(this.iBtnOrange2_Click);
            this.iBtnOrange3.Click += new ImageClickEventHandler(this.iBtnOrange3_Click);
            this.iBtnOrange5.Click += new ImageClickEventHandler(this.iBtnOrange5_Click);
            this.iBtnOrange6.Click += new ImageClickEventHandler(this.iBtnOrange6_Click);
            this.iBtnOrange7.Click += new ImageClickEventHandler(this.iBtnOrange7_Click);
            this.iBtnOrange8.Click += new ImageClickEventHandler(this.iBtnOrange8_Click);
            this.TransportToolUsedDdl.SelectedIndexChanged += new EventHandler(this.TransportToolUsedDdl_SelectedIndexChanged);
            this.rptTravel.ItemCommand += new RepeaterCommandEventHandler(this.rptTravel_ItemCommand);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            ListItem tmpItem0 = new ListItem();
            ListItem tmpItem1 = new ListItem();
            ListItem tmpItem2 = new ListItem();
            ListItem tmpItem3 = new ListItem();
            ListItem tmpItem4 = new ListItem();
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    tmpItem0.Text = "";
                    tmpItem1.Text = "飞机";
                    tmpItem2.Text = "火车";
                    tmpItem3.Text = "汽车";
                    tmpItem4.Text = "船只";
                    tmpItem0.Value = "";
                    tmpItem1.Value = "1";
                    tmpItem2.Value = "2";
                    tmpItem3.Value = "3";
                    tmpItem4.Value = "4";
                    break;

                case "en":
                    tmpItem0.Text = "";
                    tmpItem1.Text = "Plans";
                    tmpItem2.Text = "Train";
                    tmpItem3.Text = "Auto";
                    tmpItem4.Text = "Ship";
                    tmpItem0.Value = "";
                    tmpItem1.Value = "1";
                    tmpItem2.Value = "2";
                    tmpItem3.Value = "3";
                    tmpItem4.Value = "4";
                    break;
            }
            this.TransportToolUsedDdl.Items.Add(tmpItem0);
            this.TransportToolUsedDdl.Items.Add(tmpItem1);
            this.TransportToolUsedDdl.Items.Add(tmpItem2);
            this.TransportToolUsedDdl.Items.Add(tmpItem3);
            this.TransportToolUsedDdl.Items.Add(tmpItem4);
            this.lbSaveMsg.Text = string.Empty;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void RptDataBind()
        {
            this.rptTravel.DataSource = ManageCustomer.GetCustomerTravelInfo(new Guid(base.Request.QueryString["CustomerId"]));
            this.rptTravel.DataBind();
        }

        private void rptTravel_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                base.Response.Write("<script> alert('没有权限操作！') </script>");
            }
            else
            {
                Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
                if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
                {
                    base.Response.Write("<script> alert('没有权限操作！') </script>");
                }
                else
                {
                    string cmd = e.CommandName;
                    string strTmp = (string) e.CommandArgument;
                    Guid customerTravelId = new Guid(strTmp);
                    switch (cmd)
                    {
                        case "delete":
                            ManageCustomer.RemoveCustomerTravelInfoViaId(customerTravelId);
                            this.InitPage();
                            this.FillPage();
                            break;

                        case "modify":
                        {
                            CustomerTravel customerTravel = ManageCustomer.GetCustomerTravelInfoViaId(customerTravelId);
                            if (customerTravel != null)
                            {
                                try
                                {
                                    this.CouponTxb.Text = customerTravel.Coupon;
                                    this.OtherTxb.Text = customerTravel.Other;
                                    this.EditChk.Checked = true;
                                    this.Page.Session.Add("CustomerTravelId", customerTravel.CustomerTravelId.ToString());
                                }
                                catch (Exception)
                                {
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strCoupon = this.CouponTxb.Text.Trim();
            string strOther = this.OtherTxb.Text.Trim();
            try
            {
                if (this.EditChk.Checked)
                {
                    ManageCustomer.UpdateCustomerTravelInfo(new Guid(this.Page.Session["CustomerTravelId"].ToString()), strCoupon, strOther);
                    ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "UpdateTravel", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name, new Guid(base.Request.QueryString["CustomerId"]));
                }
                else
                {
                    ManageCustomer.InsertNewCustomerTravelInfo(new Guid(base.Request.QueryString["CustomerId"]), strCoupon, strOther);
                    ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "InsertTravel", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name);
                }
                this.EditChk.Checked = false;
                this.TransportToolUsedDdl.SelectedIndex = 0;
                this.CouponTxb.Text = string.Empty;
                this.OtherTxb.Text = string.Empty;
            }
            catch (ArgumentOutOfRangeException exOutOfRange)
            {
                this.lbSaveMsg.Text = exOutOfRange.Message;
                return;
            }
            catch (Exception ex)
            {
                this.lbSaveMsg.Text = ex.Message;
                return;
            }
            this.RptDataBind();
            this.lbSaveMsg.Text = "Save successfully!";
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                this.InitPage();
                this.FillPage();
                this.CouponTxb.ReadOnly = true;
                this.lbEdit.Visible = false;
                this.EditChk.Visible = false;
                this.OtherTxb.ReadOnly = true;
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeB == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbTranSportTool.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoTravelPage", "lbTranSportTool", language, path);
            this.lbCoupon.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoTravelPage", "lbCoupon", language, path);
            this.lbOther.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoTravelPage", "lbOther", language, path);
            this.lbEdit.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoTravelPage", "lbEdit", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoTravelPage", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfo.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfo_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfo_lable_2_orange.jpg";
                    this.iBtnOrange2.ImageUrl = "../images/title_CustomerInfo_lable_3_orange.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfo_lable_4_orange.jpg";
                    this.iBtnGray4.ImageUrl = "../images/title_CustomerInfo_lable_5_Gray.jpg";
                    this.iBtnOrange5.ImageUrl = "../images/title_CustomerInfo_lable_6_orange.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfo_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfo_lable_8_orange.jpg";
                    this.iBtnOrange8.ImageUrl = "../images/title_CustomerInfo_lable_9_orange.gif";
                    break;

                case "en":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfoEn.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfoEn_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfoEn_lable_2_orange.jpg";
                    this.iBtnOrange2.ImageUrl = "../images/title_CustomerInfoEn_lable_3_orange.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfoEn_lable_4_orange.jpg";
                    this.iBtnGray4.ImageUrl = "../images/title_CustomerInfoEn_lable_5_Gray.jpg";
                    this.iBtnOrange5.ImageUrl = "../images/title_CustomerInfoEn_lable_6_orange.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfoEn_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfoEn_lable_8_orange.jpg";
                    this.iBtnOrange8.ImageUrl = "../images/title_CustomerInfoEn_lable_9_orange.gif";
                    break;
            }
        }

        private void TransportToolUsedDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
            if (ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
            {
                ManageCustomer.UpdateCustomerTravelInfoOfTranToole(customerId, this.TransportToolUsedDdl.SelectedItem.Value);
            }
        }
    }
}

